function MarkInvalidate(str){
  YUI().use("node",function(Y){
    ele = Y.one("div.invalid-hint");
    if(str){
      ele.set("innerHTML",str);
      ele.setStyle("display","block");
    }
  });
}
function MarkValidate(){
  YUI().use("node",function(Y){
    Y.one("div.invalid-hint").setStyle("display","none");
  });
}

function document_load(){
  zoyoe.map = new MapInfoCore(function(){},null,null,null);
  if(zoyoe.ElementExtension){
    zoyoe.ElementExtension.BuildExtensionElements();
  }
  YUI().use("node",'event','io',function(Y){
     /*Y.on('key',function(e){
       if(e.keyCode == 13){
         zoyoe.search.PreSearch();
       }
     });
     */
     var input = Y.one("div.extension-auto-complete").one("input");
     var dom_input = Y.Node.getDOMNode(input);
     dom_input.ext_onchange = function(searchStr){
       if(zoyoe.search){
         zoyoe.search.AutoComplete(searchStr);
       }
     var patten = new RegExp("[a-zA-Z0-9]+");
      if(patten.test(searchStr)){
        MarkValidate();
      }else{
        MarkInvalidate("Invalid Query String");
      }
     }
     var types = Y.one("#restaurant-type");
     var search_option = Y.one("#match-option");
     zoyoe.InitZoyoe(false,Y);
     zoyoe.search = InitSearch(search_option,"/general",MarkValidate,MarkInvalidate);
     zoyoe.admin = InitAdmin(zoyoe);
  });
}

function Search(result){
  var filter = zoyoe.search.GetFilter();
  if(result && result.resourceSets){
    if(result.resourceSets[0].estimatedTotal = 0){
      MarkInvalidate('Address Not Found !!');
    }
    else if(result.resourceSets[0].estimatedTotal = 1){
      var resources = result.resourceSets[0].resources;
      if(resources.length == 0){
        MarkInvalidate('Address Not Found !!');
      }else if(resources.length == 1){
        zoyoe.ALERT(resources[0].address.formattedAddress);
        var c = resources[0].point.coordinates;
        zoyoe.ALERT(c);
        filter.LOCATION.x = c[0];
        filter.LOCATION.y = c[1];
        YUI().use("json-stringify","node",function (Y) {
          var filterStr = Y.JSON.stringify(filter);
          var searchStr = "";
          if (Y.one("#main-search-bar input") != null) {
            searchStr = Y.Node.getDOMNode(Y.one("#main-search-bar input")).value;
          } else {
            searchStr = "";
          }
          window.location.href = "/core/services/?"+"filter=" + filterStr;
        });
      }else{
        MarkInvalidate("Please provide more address details!");
      }
    }
    else{
      MarkInvalidate('Multi Address Found,Please Make It Clear !!');
    }
  }
}


function SearchForMobile(result){
   var filter = zoyoe.search.GetFilter();
  if(result && result.resourceSets){
    if(result.resourceSets[0].estimatedTotal = 0){
      MarkInvalidate('Address Not Found !!');
    }
    else if(result.resourceSets[0].estimatedTotal = 1){
      var resources = result.resourceSets[0].resources;
      if(resources.length == 0){
        MarkInvalidate('Address Not Found !!');
      }else if(resources.length == 1){
        zoyoe.ALERT(resources[0].address.formattedAddress);
        var c = resources[0].point.coordinates;
        zoyoe.ALERT(c);
        filter.LOCATION.x = c[0];
        filter.LOCATION.y = c[1];
        YUI().use("json-stringify","node",function (Y) {
          var filterStr = Y.JSON.stringify(filter);
          var searchStr = "";
          if (Y.one("#main-search-bar input") != null) {
            searchStr = Y.Node.getDOMNode(Y.one("#main-search-bar input")).value;
          } else {
            searchStr = "";
          }
          window.location.href = "/mobile/services/?"+"filter=" + filterStr;
        });
      }else{
        MarkInvalidate("Please provide more address details!");
      }
    }
    else{
      MarkInvalidate('Multi Address Found,Please Make It Clear !!');
    }
  }
}
